package com.example.service.impl;

import com.example.entity.FacilityClassify;
import com.example.entity.FacilityPublic;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Staffs;
import com.example.mapper.FacilityClassifyMapper;
import com.example.mapper.FacilityPublicMapper;
import com.example.mapper.StaffsMapper;
import com.example.result.CommonResult;
import com.example.service.FacilityPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;


/**
 * @author: tobed
 * @date: 2022/11/14
 * @time: 8:24
 */
@Service
public class FacilityPublicServiceImpl implements FacilityPublicService {

    @Autowired
    FacilityPublicMapper facilityPublicMapper;

    @Autowired
    FacilityClassifyMapper facilityClassifyMapper;

    @Autowired
    StaffsMapper staffsMapper;

    /*公共设备全查*/
    @Override
    public CommonResult list(FacilityPublic facilityPublic, PageRequest pageRequest) {
        MyPageHelper.startPage(pageRequest);
        List<FacilityPublic> list = facilityPublicMapper.list(facilityPublic);
        list.stream()
                .forEach(facilityPublic1 -> {
                    FacilityClassify facilityClassify = facilityClassifyMapper.getOneById(facilityPublic1.getFacilityClassify());
                    if (facilityClassify.getFacilityClassifyName()!=null)facilityPublic1.setTypeName(facilityClassify.getFacilityClassifyName());
                    if (facilityClassify.getFacilityClassifyName().isEmpty())facilityPublic1.setTypeName("");
                    String[] split = facilityPublic1.getMaintainerPeoples().split(",");
                    List<String> id = new ArrayList<>();
                    for (String s : split) {
                        id.add(s);
                    }
                    List<Staffs> listPeople = staffsMapper.listPeopleByIds(id);
                    listPeople.stream().forEach(staffs -> {
                        String names = "";
                        if (facilityPublic1.getAddsName()==null){
                            names += staffs.getStaffName();
                        } else {
                            names += facilityPublic1.getAddsName() +  ","+ staffs.getStaffName();
                        }
                        facilityPublic1.setAddsName(names);
                    });
                });
        return CommonResult.ok(MyPageHelper.getPageResult(list));
    }

    /*添加公共设备*/
    @Override
    public CommonResult createFacility(FacilityPublic facilityPublic) {
        facilityPublic.setCreateTime(new Date());
        String uu = String.valueOf(UUID.randomUUID());
        facilityPublic.setFacilityCode("KT"+uu.substring(2,8));
        int i = facilityPublicMapper.createFacility(facilityPublic);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*修改公共设备信息*/
    @Override
    public CommonResult update(FacilityPublic facilityPublic) {
        facilityPublic.setUpdateTime(new Date());
        int i = facilityPublicMapper.updata(facilityPublic);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*删除设备*/
    @Override
    public CommonResult remove(Integer id) {
        int i  = facilityPublicMapper.remove(id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

}
